package org.feng.navigation.app.tianxing;

import cn.hutool.core.util.StrUtil;
import org.feng.navigation.client.api.TianXingService;
import org.feng.navigation.client.dto.tianxing.TianXingAllNewsRequestDTO;
import org.feng.navigation.client.dto.tianxing.TianXingAllNewsResponseDTO;
import org.feng.navigation.domain.rpc.tianxing.domainservice.TianXingApiService;
import org.feng.navigation.domain.tianxing.entity.TianXingAllNewsObject;
import org.feng.navigation.rest.gatewayimpl.tianxing.TianXingConvert;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 天行数据业务服务实现
 *
 * @version v1.0
 * @author: junzi
 * @date: 2023年02月05日 16时29分
 */
@Service
public class TianXingServiceImpl implements TianXingService {
    @Resource
    private TianXingApiService tianXingApiService;
    @Resource
    private TianXingConvert tianXingConvert;

    @Override
    public TianXingAllNewsResponseDTO allNews(TianXingAllNewsRequestDTO requestDTO) {
        TianXingAllNewsObject tianXingAllNewsObject = tianXingConvert.transferAllNewsDomainObject(requestDTO);
        tianXingApiService.tianXingAllNews(tianXingAllNewsObject);

        TianXingAllNewsResponseDTO responseDTO = new TianXingAllNewsResponseDTO();
        // 发生错误
        if (StrUtil.isNotEmpty(tianXingAllNewsObject.getErrorMessage())) {
            responseDTO.setErrorMsg(tianXingAllNewsObject.getErrorMessage());
            return responseDTO;
        }
        // 处理成功信息
        List<TianXingAllNewsResponseDTO.TianXingAllNewsData> resultNewsList = tianXingAllNewsObject.getNewsList().stream().map(data -> {
            TianXingAllNewsResponseDTO.TianXingAllNewsData newsData = new TianXingAllNewsResponseDTO.TianXingAllNewsData();
            newsData.setDate(data.getDate());
            newsData.setUrl(data.getUrl());
            newsData.setTitle(data.getTitle());
            newsData.setSource(data.getSource());
            newsData.setPicUrl(data.getPicUrl());
            return newsData;
        }).collect(Collectors.toList());
        responseDTO.setNewsList(resultNewsList);
        return responseDTO;
    }
}
